/*!
 * \file frhash2_show.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void my_list_node_free(Frlist_node* node)
{
    frfree(node->data);
}
static void my_list_node_show(Frlist_node* node, char *p_outbuff, unsigned int ui_buff_size)
{
    snprintf(p_outbuff,ui_buff_size,"%s",(char *)node->data);
}

static void __case1(void)
{
    Frhash_header *p_hash_hdr = NULL;
    unsigned int ui_buff_size = 5;
    char *p_outbuff = NULL;

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,0x00,ui_buff_size);

    frhash2_show(p_hash_hdr, p_outbuff, ui_buff_size, my_list_node_show);

    CU_ASSERT( !memcmp(p_outbuff,"\0",1) );

    frfree(p_outbuff);
}

static void __case2(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    unsigned int ui_buff_size = 5;
    unsigned int ul_hash_size = 5;
    unsigned int ul_key = 0;
    char *p_outbuff = NULL;
    char *p_data = NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data,strlen("test") + 1,"%s","test");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data,strlen("case") + 1,"%s","case");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,0x00,ui_buff_size);

    frhash2_show(p_hash_hdr, p_outbuff, ui_buff_size, NULL);

    CU_ASSERT( !memcmp(p_outbuff,"\0",1) );

    frhash2_free(p_hash_hdr,my_list_node_free);
    frfree(p_outbuff);
    frfree(p_hash_hdr);
}

static void __case3(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    unsigned int ui_buff_size = 1;
    unsigned int ul_hash_size = 5;
    unsigned int ul_key = 0;
    char *p_outbuff = NULL;
    char *p_data = NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data,strlen("test") + 1,"%s","test");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data,strlen("case") + 1,"%s","case");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,'\0',ui_buff_size);
    frhash2_show(p_hash_hdr, p_outbuff, ui_buff_size, my_list_node_show);

    CU_ASSERT( !memcmp(p_outbuff,"\0",1) );

    frhash2_free(p_hash_hdr,my_list_node_free);
    frfree(p_outbuff);
    frfree(p_hash_hdr);
}

static void __case4(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    unsigned int ui_buff_size = 5;
    unsigned int ul_hash_size = 5;
    unsigned int ul_key = 0;
    char *p_outbuff = NULL;
    char *p_data = NULL;
    char *p_check = NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data,strlen("test") + 1,"%s","test");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data,strlen("case") + 1,"%s","case");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,'\0',ui_buff_size);

    frhash2_show(p_hash_hdr, p_outbuff, ui_buff_size, my_list_node_show);

    p_check = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_check,strlen("test") + 1,"%s","test");

    CU_ASSERT_STRING_EQUAL(p_outbuff,p_check);

    frhash2_free(p_hash_hdr,my_list_node_free);
    frfree(p_outbuff);
    frfree(p_hash_hdr);
    frfree(p_check);

}

static void __case5(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    unsigned int ui_buff_size = 25;
    unsigned int ul_hash_size = 5;
    unsigned int ul_key = 0;
    char *p_outbuff = NULL;
    char *p_check = NULL;
    char *p_data = NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data,strlen("test") + 1,"%s","test");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data,strlen("case") + 1,"%s","case");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,'\0',ui_buff_size);

    frhash2_show(p_hash_hdr, p_outbuff, ui_buff_size, my_list_node_show);

    p_check = (char *)frmalloc(strlen("testcase") + 1);
    snprintf(p_check,strlen("testcase") + 1,"%s","testcase");

    CU_ASSERT_STRING_EQUAL(p_outbuff,p_check);

    frhash2_free(p_hash_hdr,my_list_node_free);
    frfree(p_outbuff);
    frfree(p_hash_hdr);
    frfree(p_check);

}
static void __case6(void)
{
    Frhash_header *p_hash_hdr = NULL;
    unsigned int ui_buff_size = 5;
    char *p_outbuff = NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    p_hash_hdr->p_list_array = NULL;

    p_outbuff = (char *)frmalloc(ui_buff_size);
    memset(p_outbuff,'\0',ui_buff_size);

    frhash2_show(p_hash_hdr, p_outbuff, ui_buff_size, my_list_node_show);

    CU_ASSERT( !memcmp(p_outbuff,"\0",1) );

    frfree(p_outbuff);
    frfree(p_hash_hdr);

}


int add_suite_frhash2_show(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 frhash2_show()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);

    return FR_SUCC;
}
